import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {getallarchiveurl,getrighttopurl,getrightbottom} from '../services'
export interface IndexModelState {
  name: string;
  list:Array<object>;
  keys:string[];
  righttop:Array<object>;
  rightbottom:Array<object>
}

export interface IndexModelType {
  namespace: 'yjsarchive';
  state: IndexModelState;
  effects: {
    getallarticleurl:Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'yjsarchive',

  state: {
    name: 'q',
    list:[],
    keys:[],
    righttop:[],
    rightbottom:[],
  },

  effects: {
    // call链接请求
    // put推出数据
    // payload存储要修改的数据
  
    *getallarticleurl({ payload }, { call, put }) {
      let result =yield call(getallarchiveurl); 
      let result2 =yield call(getrighttopurl);
      let result3=yield call(getrightbottom);
      console.log("righturl2",result2.data);
      let keys=[];
        for (const key in result.data) {
          keys.unshift(key);
        }
      yield put({
        type:'save',
        payload:{
          list:result.data,
          keys,
          righttop:result2.data,
          rightbottom:result3.data,
        }
      })
    },
    
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default IndexModel;